package cj.web.portal.controllers;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import cj.web.admin.constants.Constants;
import cj.web.portal.service.MemberVoService;

@Path("")
public class IndexVoController extends BaseVoController{
	
	@Autowired
	private MemberVoService memberService;
	
	/**
	 * 登录
	 * @return
	 */
	@Get("login")
	public String login(){
		return "/views/portal/login.vm";
	}
	
	@Get("logout")
    public String logout(HttpServletRequest request,HttpServletResponse response){
		removeCookie(response, "memberId");
		removeCookie(response, "phone");
		return "r:";
    }
	
	@Get("index")
	public String index(Model model,@Param("phone") String phone, 
			HttpServletRequest request,HttpServletResponse response){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		List<Map> replis = memberService.getMemberByPhone(phone);
		if(replis!=null && replis.size()>0){
			String memberId = replis.get(0).get("id").toString();
			setCookie(response, "memberId", memberId);
			setCookie(response, "phone", phone);
		}else{
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
			jo.put("returnCode", Constants.returnObj.returnCode.code_2);
		}
		
		return "@"+jo.toString();
	}

	/**
	 * 首页
	 * @param model
	 * @param request
	 * @return
	 */
	@Get("")
	public String home(Model model, HttpServletRequest request, HttpServletResponse response){
		List<Map> rankinglis = memberService.getMemberAll(0, 10);
		model.add("rankinglis", rankinglis);
		return "/views/portal/index.vm";
	}
}